import { createApp } from 'vue'
import { createPinia } from 'pinia';
import './style.css'
import App from './App.vue'
import router from './router'

import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';

import { useUserStore } from './store/useUserstore';
import { useAdminStore } from './store/useAdminstore';
const app = createApp(App);
const pinia = createPinia();
app.use(pinia);
app.use(router);
app.use(ElementPlus);

const userStore = useUserStore();
const adminStore = useAdminStore();
const token = localStorage.getItem("token");
const userInfo = localStorage.getItem("userInfo");
const adminInfo = localStorage.getItem("adminInfo");

if (token) {
  userStore.setToken(token);
}

if (userInfo) {
  try {
    const parsedUserInfo = JSON.parse(userInfo);
    userStore.setUserInfo(parsedUserInfo);
  } catch (error) {
    console.error("Failed to parse userInfo from localStorage:", error);
  }
}

if (adminInfo) {
  try {
    const parsedAdminInfo = JSON.parse(adminInfo);
    adminStore.setAdminInfo(parsedAdminInfo);
  } catch (error) {
    console.error("Failed to parse adminInfo from localStorage:", error);
  }
}

app.mount('#app');